package br.odb.bzk3.midp;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;

/**
 * @author daniel
 */
public class AppStart extends MIDlet implements CommandListener,
		ItemStateListener {
	// ------------------------------------------------------------------------------
	EngineView iGameEngine;
	Thread painter;
	List mapNames;

	// ------------------------------------------------------------------------------
	public AppStart() {
		mapNames = new List("Select map to load", Choice.IMPLICIT);
		// droid hunter levels
		mapNames.append("riddler1.level", null);
		mapNames.append("simple.level", null);
		mapNames.append("level_1_3.will", null);
		mapNames.append("level_1_4.will", null);
		// life on lies levels
		mapNames.append("level_2_1.will", null);
		mapNames.append("level_2_2.will", null);
		mapNames.append("level_2_3.will", null);
		mapNames.append("level_2_4.will", null);
		mapNames.append("level_2_5.will", null);
	}

	// ------------------------------------------------------------------------------
	public void startApp() {
		Display display = Display.getDisplay(this);
		display.setCurrent(mapNames);
		mapNames.setCommandListener((CommandListener) this);
	}

	// ------------------------------------------------------------------------------
	public void pauseApp() {
		if (iGameEngine != null)
			iGameEngine.Pause();
	}

	// ------------------------------------------------------------------------------
	public void destroyApp(boolean unconditional) {
		iGameEngine = null;
	}

	// ------------------------------------------------------------------------------
	public void commandAction(Command c, Displayable d) {
		if (d == mapNames) {
			int index = mapNames.getSelectedIndex();
			iGameEngine = new EngineView(index);
			Display.getDisplay(this).setCurrent(
					(Canvas) iGameEngine.GetRender());
			painter = new Thread(iGameEngine);
			painter.start();
			painter.setPriority(Thread.MIN_PRIORITY);
		}
	}

	// ------------------------------------------------------------------------------
	public void itemStateChanged(Item item) {
	}
	// ------------------------------------------------------------------------------
}
